package com.sylar.happyreader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.sylar.happyreader.db.Crud;
import com.sylar.happyreader.tools.Config;

public class MoreActivity extends Activity{
	private ListView moreList;
	private PopupWindow mPop;
	private TextView textView;
	private LinearLayout moreLayout;
//	private AdViewLayout adviewLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.morelist);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		
		initPopView();
		
		try {
			Crud crud = new Crud(this);
			crud.delBookHisoryCache();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		moreList = (ListView) findViewById(R.id.item_list);
		
		moreLayout = (LinearLayout) findViewById(R.id.MoreLayout);
//		adviewLayout = (AdViewLayout) findViewById(R.id.adview_ayout);
		
		String[] mores = this.getResources().getStringArray(R.array.more);
//		UMFeedbackService.enableNewReplyNotification(this, NotificationType.AlertDialog);
		moreList.setAdapter(new MoreAdapter(this, 0,mores));
		moreList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				switch (position) {
				case 0:
					//Uri.parse(Config.teamHomeUrl)
					Intent viewIntent0= new Intent(MoreActivity.this,WebActivity.class);
					viewIntent0.putExtra("url", Config.teamHomeUrl);
					startActivity(viewIntent0);
					break;
				case 1:
					//Uri.parse(Config.teamAboutUrl)
					Intent viewIntent1= new Intent(MoreActivity.this,WebActivity.class);
					viewIntent1.putExtra("url", Config.teamAboutUrl);
					startActivity(viewIntent1);
					break;
				case 2:
					textView.setText(R.string.Drm_Info);
					mPop.showAtLocation(parent, Gravity.CENTER, 0, 0);
					break;
				case 3:
					textView.setText(R.string.Version_Info);
					mPop.showAtLocation(parent, Gravity.CENTER, 0, 0);
					break;
				case 4:
					textView.setText(R.string.contact);
					mPop.showAtLocation(parent, Gravity.CENTER, 0, 0);
					break;
					
				case 5:
					textView.setText(R.string.qq);
					mPop.showAtLocation(parent, Gravity.CENTER, 0, 0);
					break;
				case 6:
//					UMFeedbackService.openUmengFeedbackSDK(MoreActivity.this);
					break;
				case 7:
					Intent viewIntent2= new Intent(MoreActivity.this,BookHisoryActivity.class);
					startActivity(viewIntent2);
					break;

				default:
					break;
				}
			}
		});
//		initAirAD(moreLayout);
		
	}
	
	private void initPopView() {
		View contentView = getLayoutInflater().inflate(R.layout.moreview, null);
		textView = (TextView) contentView.findViewById(R.id.moreView);
		if (mPop == null) {
			mPop = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			mPop.setBackgroundDrawable(new BitmapDrawable());
			mPop.setFocusable(true);
			mPop.setAnimationStyle(R.style.PopupAnimation);
		}

		contentView.setFocusableInTouchMode(true);
		contentView.setOnKeyListener(new OnKeyListener() {

			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
					if (mPop != null) {
						mPop.dismiss();
					}
					return true;
				}
				return false;
			}
		});

		if (mPop.isShowing()) {
			mPop.dismiss();
		}
	}
	
	class MoreAdapter extends ArrayAdapter<String>{
		
		public MoreAdapter(Context context,  int textViewResourceId, String[] objects) {
			super(context,textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			MoreHolder holder = null;
			if (convertView == null) {
				convertView = MoreActivity.this.getLayoutInflater().inflate(R.layout.text_row2, parent, false);
				holder = new MoreHolder();
				holder.text = (TextView) convertView.findViewById(R.id.item_title1);
				convertView.setTag(holder);
			} 
			else {
				holder = (MoreHolder) convertView.getTag();
			}
			holder.text.setText(getItem(position));
			return convertView;
		}
		
	}
	
	class MoreHolder {
		public TextView text;
	}
	
	@Override
	protected void onPause() {
//		MobclickAgent.onPause(this);
		super.onPause();
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
	}
	@Override
	protected void onResume() {
		super.onResume();
		
//		MobclickAgent.onResume(this);
	}
}
